import sys
import ast

import re

from typing import List

from elasticsearch import Elasticsearch
from tqdm import tqdm

from src.base import Common, Consts
from src.base.Report import Report
from src.db.CcxDataCenterDb import CcxDataCenter
from src.db.FCDB import FCDB
from src.db.ProdDb import ProdDb
from src.tasks.enterpriseToEs.EntEnterprise import EntEnterprise
from src.tasks.enterpriseToEs.SyncEnterpriseBaseInfo import SyncEnterpriseBaseInfo
from src.tasks.enterpriseToEs.SyncEnterpriseNameList import SyncEnterpriseNameList
from src.tasks.enterpriseToEs.SyncEnterpriseRatingData import SyncEnterpriseRatingData
from src.tasks.enterpriseToEs.SyncEnterpriseSecuritiesData import SyncSecuritiesData
from src.tasks.enterpriseToEs.SyncFinanceIndicator import SyncFinanceIndicator
from src.tasks.enterpriseToEs.SyncRegionIndicator import SyncRegionIndicator
from src.utils import BatchUtils
from src.utils.LogUtils import Logger
from src.tasks.enterpriseToEs.SyncNegativeInfo import InitNegativeInfo

es = Elasticsearch(
    [{'host': Consts.elasticsearch_url, 'port': Consts.elasticsearch_port}], timeout=3600
)


def writedb(self, enterpriseList: List[EntEnterprise]):
    self.logger.info("写入数据库 开始")

    # 主体分类缺失的，一律为 T005:其他
    for ent in enterpriseList:
        if ent.ent_type is None:
            ent.ent_type = "T005"

    updateDatas = list(ent.__dict__ for ent in enterpriseList)
    BatchUtils.batchUpdate(self.__writedb__, updateDatas, self.size)
    self.logger.info("写入数据库 完成")


def writeMainEntList(enterpriseList: List[EntEnterprise]):
    es.indices.create(index=Consts.es_enterpriseList_index, ignore=400)
    for item in tqdm(enterpriseList, desc="es数据库保存/更新中...", position=0):
        item = dict(item)
        body = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "credit_code.keyword": item.get("credit_code")
                            }
                        },
                        {
                            "term": {
                                "year": item.get("year")
                            }
                        }
                    ]
                }
            },
            "track_total_hits": True
        }
        queryResult = es.search(index=Consts.es_enterpriseList_index, body=body)
        if len(queryResult['hits']['hits']) > 0:
            for updateItem in queryResult['hits']['hits']:
                es.index(index=Consts.es_enterpriseList_index, doc_type='test-type', id=updateItem['_id'],
                         body=item)
        else:
            es.index(index=Consts.es_enterpriseList_index, doc_type='test-type', body=item)


# 查询所有的企业名单
def queryAllEnterpriseList():
    body = {
        "size": 10000,
        "query": {
            "match_all": {}
        },
        "_source": ['credit_code', 'platform_region_code', 'ent_type'],
        "sort": [{
            "id": "asc"
        }]
    }
    data = es.search(index=Consts.es_enterpriseList_index, doc_type="test-type", body=body)
    a = []
    while True:
        for i in data['hits']['hits']:
            if i['_id'] in a:
                continue
            a.append(i)
        if data['hits']['hits']:
            after = data['hits']['hits'][-1]['sort']
        else:
            print('查完')
            break

        body.update({
            "search_after": after
        })
        data = es.search(index=Consts.es_enterpriseList_index, doc_type="test-type", body=body)
    return a


def main(logger, localDb, dataCenterDb, fcdb, useCache):

    # 1: 同步主体名单
    nameList = SyncEnterpriseNameList(logger=logger, localDb=localDb, fcdb=fcdb, useCache=useCache)

    enterpriseList = nameList.getEnterpriseList()

    # 更新主体基础信息
    baseInfo = SyncEnterpriseBaseInfo(logger=logger, localDb=localDb, dataCenterDb=dataCenterDb, fcdb=fcdb,
                                      useCache=useCache)
    baseInfo.update(enterpriseList)

    # 更新评级信息
    ranting = SyncEnterpriseRatingData(logger=logger, localDb=localDb, dataCenterDb=dataCenterDb, fcdb=fcdb)
    ranting.update(enterpriseList)

    # 更新存续债
    securities = SyncSecuritiesData(logger=logger, localDb=localDb, fcdb=fcdb)
    securities.update(enterpriseList)

    # 写入主企业名单
    # 写入到数据库
    baseInfo.writedb(enterpriseList)
    #
    # entList = nameList.__queryDataForEs__();
    # deb = nameList.__queryDebentureBase_V2__()
    # deb_dict = {key: value for key, value in deb}
    # my_list = []
    # credits = []
    # comps = []
    # count = 0
    # for x in entList:
    #     comps.append(x['comp_code'])
    #     credits.append(x['credit_code'])
    #     if x['ent_type'] == 'T002':
    #         my_list = my_list + nameList.__queryReportT002DataByYear__(x)
    #     else:
    #         my_list = my_list + nameList.__queryReportT001DataByYear__(x)
    #
    #
    # tcr0033s = nameList.__queryTCR0033_V2__(comps)
    # dfltinfos = nameList.__queryDfltinfo_V2__(comps);
    # nonStandards = nameList.__queryBaseNonStandard_V2__(credits)
    # listeds = nameList.__queryListed_V2__(credits)
    #
    # print(1)
    #
    # for x in my_list:
    #     if str(tcr0033s[0][0]).split(",").__contains__(x['comp_code']):
    #         x['tcr0033'] = 'TRUE';
    #     else:
    #         x['tcr0033'] = 'FALSE';
    #     if str(nonStandards[0]['credit_code']).split(",").__contains__(x['credit_code']):
    #         x['nonStandard'] = 'TRUE';
    #     else:
    #         x['nonStandard'] = 'FALSE';
    #     if str(dfltinfos[0][0]).split(",").__contains__(x['comp_code']):
    #         x['dfltinfo'] = 'TRUE';
    #     else:
    #         x['dfltinfo'] = 'FALSE';
    #
    #     if str(listeds[0]['credit_code']).split(",").__contains__(x['credit_code']):
    #         x['listed'] = 'TRUE';
    #     else:
    #         x['listed'] = 'FALSE';
    #
    #
    #     if deb_dict.__contains__(x['credit_code']):
    #         x['debSum'] = deb_dict[x['credit_code']]
    #
    # # 写入es
    # writeMainEntList(my_list)


# 程序入口
if __name__ == "__main__":
    logger = Logger()
    localDb = ProdDb(logSql=False, autocommit=True)
    dataCenterDb = CcxDataCenter(logSql=False, autocommit=True)
    fcdb = FCDB(logSql=False, autocommit=True)
    useCache = False
    type = Common.isCommandParam(sys.argv, 'type')
    if type['exist']:
        enterpriseList = queryAllEnterpriseList()
        if '1' == type['value']:
            # 财务指标初始化
            indicator = SyncFinanceIndicator(logger, localDb, dataCenterDb, fcdb, es, enterpriseList)
            indicator.update();
        if '2' == type['value']:
            # 查询并初始化负面信息
            negativeInfo = InitNegativeInfo(logger, localDb, dataCenterDb, fcdb, es, enterpriseList)
            negativeInfo.update()
        if '3' == type['value']:
            # 查询并初始化区域指标
            regionIndicator = SyncRegionIndicator(logger, localDb, dataCenterDb, fcdb, es, enterpriseList)
            regionIndicator.update()
    else:
        main(logger, localDb, dataCenterDb, fcdb, useCache)
